<template>
  <div class="mod-category-dialog">
    <el-dialog title="新增三级菜单" :visible.sync="dialogFormVisible" :before-close="closeDialog">
      <el-form :model="form" :rules="rules" ref="ruleForm" :label-width="formLabelWidth" class="ruleForm">
        <el-form-item v-if="!isAddType()" label="分类ID" prop="catId">
          <el-input v-model="form.catId" readonly class="ruleForm-input" />
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" class="ruleForm-input" />
        </el-form-item>
        <el-form-item label="父分类id" prop="parentCid">
          <el-input v-model="form.parentCid" readonly class="ruleForm-input" />
        </el-form-item>
        <el-form-item label="层级" prop="catLevel">
          <el-input v-model="form.catLevel" class="ruleForm-input" />
        </el-form-item>
        <el-form-item label="是否显示" prop="showStatus">
          <el-input v-model="form.showStatus" class="ruleForm-input" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" class="ruleForm-input" />
        </el-form-item>
        <el-form-item label="图标地址" prop="icon">
          <el-input v-model="form.icon" class="ruleForm-input" />
        </el-form-item>
        <el-form-item label="计量单位" prop="productUnit">
          <el-input v-model="form.productUnit" class="ruleForm-input" />
        </el-form-item>
        <el-form-item label="商品数量" prop="productCount">
          <el-input v-model="form.productCount" class="ruleForm-input" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="closeDialog">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    formLabelWidth: {
      type: String,
      default: '120px'
    },
    sign: {
      type: String,
      default: 'ADD'
    }
  },
  data () {
    return {
      form: {
        catId: '',
        name: '',
        parentCid: '',
        catLevel: '',
        showStatus: '',
        sort: '',
        icon: '',
        productUnit: '',
        productCount: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入活动名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        region: [
          {required: true, message: '请选择活动区域', trigger: 'change'}
        ],
        date1: [
          {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
        ],
        date2: [
          {type: 'date', required: true, message: '请选择时间', trigger: 'change'}
        ],
        type: [
          {type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change'}
        ],
        resource: [
          {required: true, message: '请选择活动资源', trigger: 'change'}
        ],
        desc: [
          {required: true, message: '请填写活动形式', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    isAddType () {
      return this.sign === 'ADD'
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    closeDialog () {
      this.$emit('closeDialog')
    }
  }
}
</script>

<style scoped>
  .ruleForm-input {
    width: 220px;
  }
</style>
